/**
 *测试StringBuild线程不安全
 * 多线程同事操作StringBuild 会出现脏数据
 *
 *
 * run程序显示为----------
 * ___#_###
 ###_#___
 ___#_###
 ###_#___
 ___#_###
 ###_#__
 */
public class myStringBuffer {
    public static void main(String args[]){
        StringBuilder stringBuilder =new StringBuilder("####____");
        myThread m1 =new myThread(stringBuilder);
        myThread m2 =new myThread(stringBuilder);
        new Thread(m1).start();
        new Thread(m2).start();
    }
}
class myThread implements Runnable{
    StringBuilder stringBuilder=null;
    int times =300;
    public myThread(StringBuilder stringBuilder) {
        this.stringBuilder = stringBuilder;
    }

    @Override
    public void run() {
        for (int i=0;i <times;i++){
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            stringBuilder.reverse();
            System.out.println(stringBuilder);
        }
    }
}
